﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>编辑单项选择题</title>
    <link href="../css/main.css" rel="stylesheet" type="text/css" />
    <script src="../js/jquery-1.5.2.min.js" type="text/javascript"></script>
    <link href="../css/jquery.loadmask.css" rel="stylesheet" type="text/css" />
    <script src="../js/ligerui.min.js" type="text/javascript"></script>
     <script src="../Js/json2.js" type="text/javascript"></script>
     <script src="../Js/jquery.loadmask.min.js" type="text/javascript"></script>
     <script src="../Js/conmon.js" type="text/javascript"></script>
    <script type="text/javascript">
        var topicID;
        var anserIndex = 0;
        var pWind;
        $(function () {
            pWind = parent.window.parent.window.getWindow();
            topicID = (pWind != null ? pWind.TopicID : null);
            if (topicID == null)
                topicID = 0;
            var IsView = (pWind != null ? pWind.IsView : false);
            if (IsView)
                $("#btnPost").css("display", "none");

            //获取职务列表
            $.getJSON('../WebServer/TopicMng.ashx?Action=RoleList', {Rnd: Math.random()}, function (data) {
                $.each(data, function (i, item) {
                    var str = "<div style='float:left;width:180px;padding:2px;'>";
                    if (i % 3 == 0 && i != 0)
                        str = "<div style='clear:both;float:left;width:180px;padding:2px;'>";
                    str += "<input type='checkbox' name='chkRole' id='chkRole" + i + "' value='" + item.id + "'/><label for='chkRole" + i + "'>" + item.name + "</label>";
                    str += "</div>";
                    $("#divTopicRole").append(str);
                });
            });
            //topicID=0表示新增,否则修改
            if (topicID == 0) {
                AddAnswer(4);
            }
            else {
                //取题目数据
                var params = { TopicID: topicID, Rnd: Math.random() };
                $.getJSON('../WebServer/TopicMng.ashx?Action=TopicByID', params, function (data) {
                    if (data != null) {
                        $("#txtTitle").val(data.title);
                    }
                });
                //选项数据
                $.getJSON('../WebServer/TopicMng.ashx?Action=AnswerListByID', params, function (data) {
                    if (data != null) {
                        var checkedOld = false;
                        $.each(data, function (i, item) {
                            anserIndex++;
                            var checkedStr = ((item.right == 1 && !checkedOld) ? 'checked="true"' : '');
                            //alert(item.right + checkedStr);
                            $("#tableTopicAnser").append('<tr><td>' + anserIndex + '、<input type="text" answerID="' + item.id + '" class="l-text" id="txtAnswer' + anserIndex + '" style="width:80%" value="' + item.answer + '" />&nbsp;&nbsp;<input type="radio" id="radioAnswer' + anserIndex + '" name="radioAnswer" ' + checkedStr + '" /><label for="radioAnswer' + anserIndex + '">正确答案</label><p style="border-bottom:1px dashed gray;padding-top:5px;width:100%" ></p></td></tr>');
                            checkedOld = checkedOld || item.right == 1;
                        });
                    }
                });
                //职务列表
                $.getJSON('../WebServer/TopicMng.ashx?Action=TopicRoleListByID', params, function (data) {
                    if (data != null) {
                        $.each(data, function (i, item) {
                            var checkedRole = $("#divTopicRole :checkbox[value='" + item + "']");
                            if (checkedRole != null)
                                checkedRole.attr("checked", true);
                        });
                    }
                });
            }
            $("#btnClose").click(function () { closeRefresh(false, ""); });
        });

        function AddAnswer(count) {
            for (var i = 0; i < count; i++) {
                anserIndex += 1;
                $("#tableTopicAnser").append('<tr><td>' + anserIndex + '、<input type="text" answerID="0" class="l-text" id="txtAnswer' + anserIndex + '" style="width:80%" />&nbsp;&nbsp;<input type="radio" id="radioAnswer' + anserIndex + '" name="radioAnswer" /><label for="radioAnswer' + anserIndex + '">正确答案</label>&nbsp;<input type="button" value="删除" onclick="DelAnswer(this,0);" class="l-button" style="display:inline;width:40px"><p style="border-bottom:1px dashed gray;padding-top:5px;width:100%" ></p></td></tr>');
            }
        }

        function DelAnswer(obj) {
            if ($(obj).parent()) {
                $(obj).parent().next().remove();
                $(obj).parent().remove();
            }
        }
        var cuselectnodes = [];
        function SaveData() {
            if ($("#txtTitle").val() == "") {
                showErrorTip("请填写题目！");
                return;
            }
            var ctrlAnswers = $("#tableTopicAnser :text[id^='txtAnswer'][value!='']");
            if (ctrlAnswers.length < 2) {
                showErrorTip("必须填写不少于两个选项数据！");
                return;
            }
            var ctrlChkAnswers = $("#tableTopicAnser :radio[id^='radioAnswer'][checked=true]");
            if (ctrlChkAnswers.length == 0) {
                showErrorTip("必须选择一个正确答案！");
                return;
            }
            var ctrlRoles = $("#divTopicRole :checkbox[id^='chkRole'][checked=true]");
            if (ctrlRoles.length == 0) {
                showErrorTip("请选择题库.");
                return;
            }
            var topicData = { id: topicID, title: $("#txtTitle").val(), type: 1 };
            var anserList = new Array();
            var roleList = new Array();
            var checkedAll = false;
            $.each(ctrlAnswers, function (i, item) {
                var checked = $(item).next(":radio[id^='radioAnswer']").attr("checked");
                var ansnwer = { id: $(item).attr("answerID"), answer: $(item).val(), code: 0, topic: topicID, right: checked ? 1 : 0 };
                checkedAll = checkedAll || checked;
                anserList.push(ansnwer);
            });
            if (!checkedAll) {
                showErrorTip("必须选择一个选项内容不为空的正确答案！");
                return;
            }
            $.each(ctrlRoles, function (i, item) {
                roleList.push($(item).val());
                cuselectnodes.push({ text:$(item).next("label").text(), id: $(item).val()});
            });
            $("#divContent").mask("数据处理中，请稍候......");
            var data = { topic: JSON2.stringify(topicData), anserList: JSON2.stringify(anserList), roleList: JSON2.stringify(roleList), Rnd: Math.random() };
            $.post("../WebServer/TopicMng.ashx?Action=SaveTopic", data, function (msgData) {
                var data = eval("(" + msgData + ")");
                $("#divContent").unmask();
                if (data.IsSuccess == 1) {
                    showAlertOk("保存成功");

                    if (pWind) {
                        pWind.loadGrid();
                       
                      if(cuselectnodes)
                          pWind.reflushTree(cuselectnodes);
                  }
                  closeRefresh(false, "");
                }
                else
                    showWarningTip(data.Msg);
            });
        }
    </script>
    <style type="text/css">
        .table{background-color:#84A0C4;border:1px solid #84A0C4;}
        .td{background-color:#ffffff;}
    </style>
</head>
<body>
    <div id="divContent">
    <table width="100%" cellpadding="1px" cellspacing="1px" class="table">
        <tr style="height:30px">
            <td colspan="2" align="center" class="td" style="font-weight:bold;">编辑单项选择题</td>
        </tr>
        <tr>
            <td class="td" style="width:10%;" align="right">题目：</td>
            <td class="td" style="padding:3px"><textarea id="txtTitle" style="width:99%" rows="3" cols="0" class="l-textarea"></textarea></td>
        </tr>
        <tr>
            <td class="td" style="width:10%;" align="right">选项：</td>
            <td class="td" style="padding:3px" id="tdTopicAnswer">
                <table width="100%" cellpadding="0" cellspacing="0" id="tableTopicAnser">
                    <tr>
                        <td style="padding-bottom:10px;"><input type="button" id="btnAddAnser" onclick="AddAnswer(4);" value="增加选项" class="l-button" style="display:inline;width:60px;height:22px;" /></td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td class="td" style="width:10%;" align="right">职务：</td>
            <td class="td" style="padding:3px">
                <div id="divTopicRole">
                </div>
            </td>
        </tr>
    </table>
    <table width="100%" cellpadding="0" cellspacing="0" style="height:100%;">
        <tr style="height:30px">
            <td  align="center">
                <input type="button" id="btnPost" value="保存" onclick="SaveData();" class="l-button" style="display:inline" />
                <input type="button" id="btnClose" value="关闭"class="l-button" style="display:inline" />
            </td>
        </tr>    
    </table>
    </div>
</body>
</html>
